{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import json"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['.DS_Store', 'dh_msra.txt']"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "os.listdir(\"corpus\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"corpus/dh_msra.txt\", \"r\", encoding=\"utf-8\") as f:\n",
    "    all_lines = [i for i in f.readlines()]\n",
    "# all_lines:\n",
    "# ['当\\tO\\n',\n",
    "#  '希\\tO\\n',\n",
    "#  '望\\tO\\n',..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "all_texts, all_labels = [], []\n",
    "texts_cache, labels_cache = [], []\n",
    "for line in all_lines:\n",
    "    if len(line) == 1:\n",
    "        all_texts.append(texts_cache)\n",
    "        all_labels.append(labels_cache)\n",
    "        texts_cache, labels_cache = [], []\n",
    "    else:\n",
    "        char, tag = line.split()\n",
    "        texts_cache.append(char)\n",
    "        labels_cache.append(tag)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "55389\n",
      "55389\n"
     ]
    }
   ],
   "source": [
    "print(len(all_texts))\n",
    "print(len(all_labels))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"train_data.txt\", \"w+\", encoding=\"utf-8\") as f:\n",
    "    for text, label in zip(all_texts, all_labels):\n",
    "        f.write(str({\"text\": text, \"label\": label}))\n",
    "        f.write(\"\\n\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "def extend_lis(lis):\n",
    "    extended = []\n",
    "    for i in lis:\n",
    "        extended.extend(i)\n",
    "    return extended"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "texts4dict = extend_lis(all_texts)\n",
    "labels4dict = extend_lis(all_labels)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "unique_chars = list(set(texts4dict))\n",
    "unique_tags = list(set(labels4dict))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "4832\n",
      "['B-ORG', 'B-PER', 'I-PER', 'O', 'I-LOC', 'B-LOC', 'I-ORG']\n"
     ]
    }
   ],
   "source": [
    "print(len(unique_chars))\n",
    "print(unique_tags)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [],
   "source": [
    "char2idx = {\"UNK\": 0}\n",
    "for i, element in enumerate(unique_chars):\n",
    "    char2idx[element] = i + 1\n",
    "tag2idx = {element: i for i, element in enumerate(unique_tags)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open(\"./char2idx.json\", \"w+\", encoding=\"utf-8\") as f:\n",
    "    f.write(json.dumps(char2idx, ensure_ascii=False))\n",
    "with open(\"./tag2idx.json\", \"w+\", encoding=\"utf-8\") as f:\n",
    "    f.write(json.dumps(tag2idx, ensure_ascii=False))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
